package cn.eoeit.mmy.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.eoeit.mmy.bean.Pagination;
import cn.eoeit.mmy.bean.RespCode;
import cn.eoeit.mmy.bean.Response;
import cn.eoeit.mmy.model.DeviceInfo;
import cn.eoeit.mmy.model.Factory;
import cn.eoeit.mmy.model.RoleModel;
import cn.eoeit.mmy.model.Seller;
import cn.eoeit.mmy.service.ISystemService;
import cn.eoeit.mmy.service.IUserService;

@Controller
@RequestMapping("/system")
public class SystemController {

	@Autowired
	private IUserService userService;

	
	@Autowired
	private ISystemService systemService;

	
	@RequestMapping("/factoryList.action")
	public String factoryList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		List<DeviceInfo> userInfos = systemService.getDeviceInfos();
		modelMap.addAttribute("datas", userInfos);
		return "factoryList";
	}

	@RequestMapping("/factoryToAdd.action")
	public String factoryToAdd(ModelMap modelMap) {
		
		return "factoryToAdd";
	}
	
	
	@RequestMapping("/deleteDevice.action")
	public  @ResponseBody Response deleteDevice(Integer uid,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		DeviceInfo user =systemService.finishDeleteDeviceInfo(uid);
		if(user!=null){
			response.setMessage("加工设备删除成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("加工设备删除失败"); 
			
		} 
		
		return response;
	}
	
	
	
	@RequestMapping("/addDeviceInfo.action")
	public  @ResponseBody Response addDeviceInfo(@RequestBody DeviceInfo user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =systemService.finishDeviceInfo(user);
		if(user!=null){
			response.setMessage("加工设备添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("加工设备添加失败"); 
			
		} 
		
		return response;
	}
	@RequestMapping("/toolsList.action")
	public String toolsList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> userInfos = userService.selectAllUsers(page, 0);
		modelMap.addAttribute("userInfos", userInfos);
		return "toolsList";
	}

	@RequestMapping("/toolsToAdd.action")
	public String toolsToAdd(ModelMap modelMap) {
		List<RoleModel>roles=userService.findAllRoleModel();
		modelMap.addAttribute("roles", roles);
		return "toolsToAdd";
	}

	@RequestMapping("/operaterList.action")
	public String operaterList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> userInfos = userService.selectAllUsers(page, 0);
		modelMap.addAttribute("userInfos", userInfos);
		return "operaterList";
	}

	@RequestMapping("/operaterToAdd.action")
	public String operaterToAdd(ModelMap modelMap) {
		List<RoleModel>roles=userService.findAllRoleModel();
		modelMap.addAttribute("roles", roles);
		return "operaterToAdd";
	}

	@RequestMapping("/sellShipList.action")
	public String sellShipList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> userInfos = userService.selectAllUsers(page, 0);
		modelMap.addAttribute("userInfos", userInfos);
		return "sellShipList";
	}

	@RequestMapping("/sellShipToAdd.action")
	public String sellShipToAdd(ModelMap modelMap) {
		List<RoleModel>roles=userService.findAllRoleModel();
		modelMap.addAttribute("roles", roles);
		return "sellShipToAdd";
	}

	@RequestMapping("/sellerList.action")
	public String sellerList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		List<Seller>userInfos = systemService.getSellers();
		modelMap.addAttribute("datas", userInfos);
		return "sellerList";
	}

	@RequestMapping("/sellerToAdd.action")
	public String sellerToAdd(ModelMap modelMap) {
	
		return "sellerToAdd";
	}
	
	
	
	
	@RequestMapping("/deleteSeller.action")
	public  @ResponseBody Response deleteSeller(Integer uid,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		Seller user =systemService.finishDeleteSeller(uid);
		if(user!=null){
			response.setMessage("销售员删除成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("销售员删除失败"); 
			
		} 
		
		return response;
	}

	@RequestMapping("/addSeller.action")
	public  @ResponseBody Response addSeller(@RequestBody Seller user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =systemService.finishAddSeller(user);
		if(user!=null){
			response.setMessage("销售员信息添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("销售员信息添加失败"); 
			
		} 
		
		return response;
	}
	
	@RequestMapping("/buyShipList.action")
	public String buyShipList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		List<Factory>userInfos = systemService.getFactorys();
		modelMap.addAttribute("datas", userInfos);
		return "buyShipList";
	}

	@RequestMapping("/buyShipToAdd.action")
	public String buyShipToAdd(ModelMap modelMap) {
		
		return "buyShipToAdd";
	}

	@RequestMapping("/buyerList.action")
	public String buyerList(ModelMap modelMap, Integer page) {
		if (page == null) {
			page = 0;
		}
		Pagination<HashMap<String,Object>> userInfos = userService.selectAllUsers(page, 0);
		modelMap.addAttribute("userInfos", userInfos);
		return "sellShipList";
	}
	

	@RequestMapping("/deleteBuyer.action")
	public  @ResponseBody Response deleteBuyer(Integer uid,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		Factory user =systemService.finishDeleteFactory(uid);
		if(user!=null){
			response.setMessage("采购员删除成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("采购员删除失败"); 
			
		} 
		
		return response;
	}

	@RequestMapping("/deleteBuyShip.action")
	public  @ResponseBody Response deleteBuyShip(Integer uid,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		Factory user =systemService.finishDeleteFactory(uid);
		if(user!=null){
			response.setMessage("采购员删除成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("采购员删除失败"); 
			
		} 
		
		return response;
	}
	
	
	@RequestMapping("/addBuyShip.action")
	public  @ResponseBody Response addBuyShip(@RequestBody Factory user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =systemService.finishAddFactory(user);
		if(user!=null){
			response.setMessage("销售员信息添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("销售员信息添加失败"); 
			
		} 
		
		return response;
	}
	@RequestMapping("/addBuyer.action")
	public  @ResponseBody Response addBuyer(@RequestBody Factory user,HttpServletRequest request){
		
		Response response=Response.getSuccess();
		user =systemService.finishAddFactory(user);
		if(user!=null){
			response.setMessage("销售员信息添加成功");
		} else{
			response.setCode(RespCode.USER_ADD_ERROR);
			response.setMessage("销售员信息添加失败"); 
			
		} 
		
		return response;
	}
	

	@RequestMapping("/buyerToAdd.action")
	public String buyerToAdd(ModelMap modelMap) {
		List<RoleModel>roles=userService.findAllRoleModel();
		modelMap.addAttribute("roles", roles);
		return "sellShipToAdd";
	}
}
